﻿// MIT License (MIT)
// 
// Copyright (c) 2014 K Ercan Turkarslan
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 

using System;

namespace Mios
{
    public abstract class MiosService
    {
        protected MiosEngine Engine { get; private set; }

        protected UInt32 DeviceId { get; private set; }

        protected String ServiceId { get; private set; }

        #region Conversion Methods

        #region FromLuup.... Methods
        protected String FromLuupString(String luupValue)
        {
            return luupValue;
        }

        protected Boolean FromLuupBoolean(String luupValue)
        {
            if ((luupValue == "0") || (luupValue == "1"))
                return (luupValue == "1");

            if (String.IsNullOrEmpty(luupValue) || (luupValue == "-1") || (luupValue == "-3"))
                // WARNING: We will ignore this case and return default<T> for now.
                return default(Boolean);

            throw new MiosFormatException(String.Format("Could not parse returned value [{0}] to Boolean!", luupValue));
        }

        protected SByte FromLuupSByte(String luupValue)
        {
            SByte retValue;
            if (SByte.TryParse(luupValue, out retValue))
                return retValue;

            if (String.IsNullOrEmpty(luupValue))
                // WARNING: We will ignore this case and return default<T> for now.
                return default(SByte);

            throw new MiosFormatException(String.Format("Could not parse returned value [{0}] to SByte!", luupValue));
        }

        protected Byte FromLuupByte(String luupValue)
        {
            Byte retValue;
            if (Byte.TryParse(luupValue, out retValue))
                return retValue;

            if (String.IsNullOrEmpty(luupValue))
                // WARNING: We will ignore this case and return default<T> for now.
                return default(Byte);

            throw new MiosFormatException(String.Format("Could not parse returned value [{0}] to Byte!", luupValue));
        }

        protected Int16 FromLuupInt16(String luupValue)
        {
            Int16 retValue;
            if (Int16.TryParse(luupValue, out retValue))
                return retValue;

            if (String.IsNullOrEmpty(luupValue))
                // WARNING: We will ignore this case and return default<T> for now.
                return default(Int16);

            throw new MiosFormatException(String.Format("Could not parse returned value [{0}] to Int16!", luupValue));
        }
        
        protected UInt16 FromLuupUInt16(String luupValue)
        {
            UInt16 retValue;
            if (UInt16.TryParse(luupValue, out retValue))
                return retValue;

            if (String.IsNullOrEmpty(luupValue))
                // WARNING: We will ignore this case and return default<T> for now.
                return default(UInt16);

            throw new MiosFormatException(String.Format("Could not parse returned value [{0}] to UInt16!", luupValue));
        }
        
        protected Int32 FromLuupInt32(String luupValue)
        {
            Int32 retValue;
            if (Int32.TryParse(luupValue, out retValue))
                return retValue;

            if (String.IsNullOrEmpty(luupValue))
                // WARNING: We will ignore this case and return default<T> for now.
                return default(Int32);

            throw new MiosFormatException(String.Format("Could not parse returned value [{0}] to Int32!", luupValue));
        }
        
        protected UInt32 FromLuupUInt32(String luupValue)
        {
            UInt32 retValue;
            if (UInt32.TryParse(luupValue, out retValue))
                return retValue;

            if (String.IsNullOrEmpty(luupValue))
                // WARNING: We will ignore this case and return default<T> for now.
                return default(UInt32);

            throw new MiosFormatException(String.Format("Could not parse returned value [{0}] to UInt32!", luupValue));
        }
        
        protected DateTime FromLuupDateTime(String luupValue)
        {
            DateTime retValue;
            if (DateTime.TryParse(luupValue, out retValue))
                return retValue;

            if (String.IsNullOrEmpty(luupValue))
                // WARNING: We will ignore this case and return default<T> for now.
                return default(DateTime);

            throw new MiosFormatException(String.Format("Could not parse returned value [{0}] to DateTime!", luupValue));
        }
        #endregion       

        #region ToLuup Methods

        protected String ToLuup(String value)
        {
            return value;
        }

        protected String ToLuup(Boolean? value)
        {
            return (value ?? false) ? "1" : "0";
        }

        protected String ToLuup(SByte? value)
        {
            return value.ToString();
        }

        protected String ToLuup(Byte? value)
        {
            return value.ToString();
        }

        protected String ToLuup(Int16? value)
        {
            return value.ToString();
        }

        protected String ToLuup(UInt16? value)
        {
            return value.ToString();
        }

        protected String ToLuup(Int32? value)
        {
            return value.ToString();
        }

        protected String ToLuup(UInt32? value)
        {
            return value.ToString();
        }

        protected String ToLuup(DateTime? value)
        {
            return value.ToString();
        }
        #endregion

        #endregion

        #region SetVariable method
        internal virtual Boolean SetVariable(String name, String value)
        {
            return false;
        }
        #endregion SetVariable method

        #region Constructor(s)

        protected MiosService(MiosEngine engine, UInt32 deviceId, String serviceId)
        {
            Engine = engine;
            DeviceId = deviceId;
            ServiceId = serviceId;
        }

        #endregion
    }
}
